package com.own.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.own.system.entity.Provider;
import com.own.system.service.ProviderService;
import com.own.system.utils.Result;
import com.own.system.vo.query.ProviderQueryVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 *  前端控制器
 * @author anonymous
 * @since 2024-08-25
 */
@RestController
@RequestMapping("/api/provider")
public class ProviderController {
    @Resource
    private ProviderService providerService;

    /**
     * 分页查询商品列表
     * @param providerQueryVo
     * @return
     */
    @GetMapping("/list")
    public Result list(ProviderQueryVo providerQueryVo) {
        //创建分页查询对象
        IPage<Provider> page = new Page<Provider>(providerQueryVo.getPageNo(),providerQueryVo.getPageSize());
        //调用分页查询方法
        providerService.findProviderListByPage(page,providerQueryVo);
        return Result.ok(page);
    }

    /**
     * 添加商品
     * @param provider
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:provider:add')")
    public Result add(@RequestBody Provider provider) {
        if (providerService.save(provider)){
            return Result.ok().message("添加成功");
        }
        return Result.error().message("添加失败");
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @CrossOrigin
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:provider:delete')")
    public Result delete(@PathVariable Long id) {
        if (providerService.removeById(id)){
            return Result.ok().message("删除成功");
        }
        return Result.error().message("删除失败");
    }

    /**
     * 修改商品
     * @param provider
     * @return
     */
    @CrossOrigin
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('sys:provider:edit')")
    public Result update(@RequestBody Provider provider) {
        if (providerService.updateById(provider)){
            return Result.ok().message("修改成功");
        }
        return Result.error().message("修改失败");
    }
}
